package riteh.eventNetwork.client;

import com.google.gson.Gson;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
 

public class C {
	
	/* Klasa C (constants) sluzi za spremanje brojcanih konstanta 
	 * i javnih funkcija koje se koriste kroz cijeli program.
	 */
	
	public static final String TAG = "MYTAG";

	public static final int ACTIVITY_TABS = 0;
	public static final int ACTIVITY_REGISTER = 1;
	public static final int ACTIVITY_HOME = 2;
	public static final int ACTIVITY_PROFILE = 3;
	public static final int ACTIVITY_CIRCLES = 4;
	public static final int ACTIVITY_EVENT = 5;
	public static final int ACTIVITY_USER_EDIT = 6;
	public static final int ACTIVITY_CIRCLE_EDIT = 7;
	public static final int ACTIVITY_NOTIFICATIONS = 8;
	public static final int ACTIVITY_SEARCH = 9;
	
	public static final int SUCCESS = 0;
	public static final int ERROR = -1;
	
	public static String SERVER_ADRESS = "http://31.147.110.137:8080/riteh.proba.server/rest/";

	public static boolean resultSuccess(Context ctx, String result, String errorMsg) {
		if (result == null) {
			Toast.makeText(ctx, ctx.getString(R.string.toastNoConnection), Toast.LENGTH_LONG).show();
		}
		
		else if (result.compareTo("-1") == 0){
			Toast.makeText(ctx, errorMsg, Toast.LENGTH_LONG).show();
		}
		else {
			return true;
		}
		return false;
	}

	public static boolean resultSuccess(Context ctx, String result[], String errorMsg) {
		if (result == null) {
			Toast.makeText(ctx, ctx.getString(R.string.toastNoConnection), Toast.LENGTH_LONG).show();
		}
		else if (result[0].compareTo("-1") == 0){
			Toast.makeText(ctx, errorMsg, Toast.LENGTH_LONG).show();
		}
		else {
			return true;
		}
		return false;
	}
	
	public static boolean resultSuccess(Context ctx, String result[][], String errorMsg) {
		if (result == null) {
			Toast.makeText(ctx, ctx.getString(R.string.toastNoConnection), Toast.LENGTH_LONG).show();
		}
		else if (result[0][0].compareTo("-1") == 0){
			Toast.makeText(ctx, errorMsg, Toast.LENGTH_LONG).show();
		}
		else {
			return true;
		}
		return false;
	}
	
	public static String getMethod0(RestClient client, String method) {	
		String response = null;
		response = executeClient(client, method);
		
		if (response == null) return null;
		//else response = response.substring(0, response.length()-1);
        
		return response;
	}
	
	public static String[] getMethod1(RestClient client, String method) {
		String response = null;
		
		response = executeClient(client, method);

        
        if (response == null) return null;
        //else response = response.substring(0, response.length()-1);

        if (response.compareTo("0") == 0) return new String[] {"0"};
        else if (response.compareTo("-1") == 0) return new String[] {"-1"};
        else {
        	Gson gson = new Gson();
    		String res[] = gson.fromJson(response, String[].class);

    		return res;
    	}
	}
	
	public static String[][] getMethod2(RestClient client, String method) {
		String response = null;
		
		response = executeClient(client, method);
		
        if (response == null) return null;
		
        if (response.compareTo("0") == 0) return new String[][] {{"0"}};
        else if (response.compareTo("-1") == 0) return new String[][]{{"-1"}};
        else {
        	Gson gson = new Gson();
        	String res[][] = gson.fromJson(response, String[][].class);
        	return res;
        }
	}
	
	public static String executeClient(RestClient client, String method) {
		String response = null;
		try {
        	client.Execute(RequestMethod.GET);
        } catch (Exception e) {
        	Log.d(C.TAG, e.getMessage());
        	e.printStackTrace();
        }
        
        try { 
        	response = client.getResponse();
        	Log.d(C.TAG, "Method: " + method + ", Response len: " + response.length() + ", Response: " + response);
        } catch (Exception e) { 
        	e.printStackTrace(); 
        }
        
        if (response == null) return null;
        else if (response.length()==0) return "0";
        else response = response.substring(0, response.length()-1);

        if (response.length() == 0) return "0";
		else if (response.compareTo("0") == 0) return "0";
		else if (response.compareTo("-1") == 0) return "-1";
		else if (response.length() > 5) {
			if (response.substring(0, 5).compareTo("<html>") == 0) return "-1";
			else if (response.startsWith("<html>")) return "-1";
			else return response;
		}
		else return response;
	}

	public static class SHA1 { 
	    private static String convertToHex(byte[] data) { 
	        StringBuffer buf = new StringBuffer();
	        for (int i = 0; i < data.length; i++) { 
	            int halfbyte = (data[i] >>> 4) & 0x0F;
	            int two_halfs = 0;
	            do { 
	                if ((0 <= halfbyte) && (halfbyte <= 9)) 
	                    buf.append((char) ('0' + halfbyte));
	                else 
	                    buf.append((char) ('a' + (halfbyte - 10)));
	                halfbyte = data[i] & 0x0F;
	            } while(two_halfs++ < 1);
	        } 
	        return buf.toString();
	    } 
	 
	    public static String compute(String text) {
		    MessageDigest md;
		    try {
		    	md = MessageDigest.getInstance("SHA-1");
		    } catch (NoSuchAlgorithmException e){
		    	e.printStackTrace();
		    	return "";
		    }
		    byte[] sha1hash = new byte[40];
		    try {
			    md.update(text.getBytes("iso-8859-1"), 0, text.length());		    	
		    } catch (UnsupportedEncodingException e){
		    	e.printStackTrace();
		    	return "";
		    }
		    sha1hash = md.digest();
		    return convertToHex(sha1hash);
	    }
	}
	
	public static void toast(Context ctx, int resId) {
		Toast.makeText(ctx, ctx.getString(resId), Toast.LENGTH_LONG).show();
	}
	
	public static void toast(Context ctx, int resId, int duration) {
		Toast.makeText(ctx, ctx.getString(resId), duration).show();
	}
	
	public static void toast(Context ctx, String text) {
		Toast.makeText(ctx, text, Toast.LENGTH_LONG).show();
	}
	
	public static void toast(Context ctx, String text, int duration) {
		Toast.makeText(ctx, text, duration).show();
	}	
	
	// Za testiranje kada nema servera stavit SERVER_OFF = true;
	//public static final boolean SERVER_OFF = false;
	public static final int TEST_ID = 1;
	
	public static final int NUM_OF_HOME_EVENTS = 5;
	public static final String HOME_EVENTS[][] = {
//   		KORISNIK	NAZIV		 						TIME		PRIORITY			RESPONSES
	{"2", 	"Ankica",	"Pokupiti krumpire s place",		"~15 min", 	"Medium", 		""},
	{"1",	"Frane",	"Donijeti lijekove iz ljekarne",	"~32 min",	"High",	"(2)"},
	{"3",	"Zdenka",	"Spremam se za šetnju",				"~2 h", 	"Low",	"(5)"},
	{"4",	"Vlado", 	"Fali nam još dvojca za boče",		"~1 h",		"Medium",		"(3)"},
	{"5",	"Branka",	"Pošto sam ja jako brbljava, mene zanima kako izgleda kada unesem jako dugačak event.\n" +
	"Joj ovo će sigurno jako ružno izgledati! :(",	"~20h 45min", "High", "(50)"}
	};

	public static final int NUM_OF_PROFILE_EVENTS = 3;
	public static final String PROFILE_EVENTS[][] = {
//	 ID		NAZIV								TRAJANJE	PRIORITET	ODGOVORI
	{"3", 	"Isao bih popiti kavu", 			"1h", 		"Nebitno",	"3"},
	{"1", 	"Treba mi prijevoz do grada",		"30 min", 	"Hitno",	"1"},
	{"2", 	"Fali mi malo šećera za kolač",		"2h", 		"Važno",	"1" },
	};
	
	public static final int NUM_OF_CIRCLES = 5;
	public static final String CIRCLES[][] = {
		{"3","Prijatelji", "#ff980000"}, 
		{"5","Obitelj", "#ff006315"}, 
		{"4","Susjedi", "#ff0000ff"}, 
		{"1", "Not in any circle", "#ff000000"},
		{"2", "Friendship requests", "#ff000000"}
	};
	
	public static final int NUM_OF_FRIENDS = 10;
	public static final String FRIENDS[][][] = {
	/*Prijatelji*/			{{"1", "Ankica"}, {"2", "Frane"}, {"3","Zdenka"}},
	/*Obitelj*/				{{"4", "Vlado"}, {"5", "Branka"}},
	/*Susjedi*/ 			{{"1", "Ankica"}, {"6", "Marija"}, {"7", "Miro"}},
	/*Not in any circle */	{{"8", "Pero"}, {"9","Ždero"}},
	/*Friendship requests*/ {{"12", "Jean Petrić"}, {"11", "Vedran Abramović"}, {"10", "Luka Filipan"}}
	};
	
	public static final int NUM_OF_NOTIFICATIONS = 3;
	public static final String NOTIFICATIONS[][] = {
		{"1", "Ankica added you to her circles! ", "2011-01-20, 16:43"},
		{"3", "Vlado has responded to your event! ", "2011-01-19, 12:20"},
		{"2", "Your event has expired.", "2011-01-18, 08:15"}
	};
	
	public static final int NUM_OF_SEARCH_USERS = 3;
	public static final String SEARCH_USERS[][] = {
		{"2", "Pero", "Ždero", "peroždero@žderići.hr"},
		{"23", "Jelena", "Pelena", "guzica.ti@zelena.hr"},
		{"43", "Denis", "Malen", "mi.je@penis.haha"}
	};
}
